import os
import tkinter

from def_pack.Tkkinter_widget import CreatNormalWidget
from def_pack.book_copy_read_find import no_search_way, simple_read_no_search_way, search_way, simple_read_search_way
from functools import partial

from def_pack.recover_data import RecoverData, RecoverData2


def choice_copy_find_way(tf1,tf2,main_win,chonum=0):
    redata = RecoverData2()
    textframelist = [tf1, tf2]
    redata.write_file_from_get_frame(textframelist)
    #小说文本路径名
    tframe1 = tf1.get("1.0", "end").replace("\n", "")
    novalfile = str(tframe1).replace("\\", "/")

    if len(novalfile)<3:
        print("没有输入文本路径，请输入后文本路径再尝试")
        return


    # 再生成全文，不搜索的
    if chonum==0:
        main_win.destroy()
        no_search_way(novalfile)

    # 看省略不搜索的
    if chonum == 1:
        main_win.destroy()
        simple_read_no_search_way(novalfile)

    # 再生成全文，搜索的
    if chonum == 2:
        beginstr2 = tf2.get("1.0", "end").replace("\n", "")
        beginstr = beginstr2.replace(" ", "")
        if len(beginstr) < 1:
            print("没有输入搜索关键词，请输入搜索关键词(段)后再尝试")
            return
        main_win.destroy()
        search_way(novalfile, beginstr)

    # 看省略搜索的
    if chonum == 3:
        beginstr = tf2.get("1.0", "end").replace("\n", "")
        if len(beginstr) < 1:
            print("没有输入搜索关键词，请输入搜索关键词(段)后再尝试")
            return
        main_win.destroy()
        simple_read_search_way(novalfile, beginstr)

    if chonum == 4:

        picture_dir = ""
        os.startfile(picture_dir)






def create_button(main_win, h_numa, l_colu_numa, text1, command, width1=6, height1=1, bg="Wheat",columnspan=1):
    if command is None:
        btn1 = tkinter.Button(main_win, text=f"{text1}", width=width1, height=height1,
                              # padx=10, pady=10,
                              bg=bg, font=("楷书", 11), fg="MediumBlue",
                              )

    else:
        btn1 = tkinter.Button(main_win, text=f"{text1}", width=width1, height=height1,
                              # padx=10, pady=10,
                              bg=bg, font=("楷书", 11), fg="MediumBlue",
                              # LightCyan，MediumBlue
                              command=command)
    btn1.grid(row=h_numa, column=l_colu_numa, padx=5, pady=10,columnspan=columnspan)
    return btn1, h_numa, l_colu_numa

class BookCopyFind():
    def __init__(self):
        self.classb = CreatNormalWidget()
        self.main_win = self.classb.creat_main_win("扒书界面选项", "Text-Docs.ico")
        self.textframe = self.classb.create_textframe_show2(self.main_win, 2, 1, 80, 4, columnspan=8)
        self.textframe2 = self.classb.create_textframe_show2(self.main_win, 4, 1, 80, 4, columnspan=8)

        self.lable = self.classb.creat_lable(self.main_win, "小说文本路径", 1, 0, columnspan=2)
        self.lable2 = self.classb.creat_lable(self.main_win, "搜索关键字", 3, 0, columnspan=2)

        self.__show_button()
        self.__get_tempframe_data()
        self.main_win.mainloop()


    def __show_button(self):
        self.search_button = create_button(self.main_win, 5, 1, "再生成全文\n不搜索",
                                           partial(choice_copy_find_way, self.textframe[0], self.textframe2[0],
                                                    self.main_win),
                                           height1=2, width1=15,
                                           bg="LightCyan")
        self.search_button = create_button(self.main_win, 5, 2, "看省略不搜索的",
                                           partial(choice_copy_find_way, self.textframe[0], self.textframe2[0],
                                                   self.main_win,chonum=1),
                                           height1=1, width1=15,
                                           bg="LightCyan")

        self.search_button = create_button(self.main_win, 5, 3, "再生成全文\n搜索的",
                                           partial(choice_copy_find_way, self.textframe[0], self.textframe2[0],
                                                   self.main_win, chonum=2),
                                           height1=2, width1=15,
                                           bg="LightCyan")

        self.search_button = create_button(self.main_win, 5, 4, "看省略搜索的",
                                           partial(choice_copy_find_way, self.textframe[0], self.textframe2[0],
                                                   self.main_win, chonum=3),
                                           height1=1, width1=15,
                                           bg="LightCyan")
        self.search_button = create_button(self.main_win, 6, 1, "在复制保存的文件夹",
                                           partial(choice_copy_find_way, self.textframe[0], self.textframe2[0],
                                                   self.main_win, chonum=4),
                                           height1=2, width1=16,
                                           bg="LightCyan")


    def __get_tempframe_data(self):
        listoo = [self.textframe[0], self.textframe2[0]]
        redata=RecoverData2()
        redata.write_data(listoo)

if __name__ == '__main__':
    BookCopyFind()
    pass